{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 分词\n",
    "# 词语 -> id\n",
    "#   matrix -> [|V|, embed_size]\n",
    "#   词语A -> id(5)\n",
    "#   词表\n",
    "\n",
    "# label -> id\n",
    "\n",
    "import sys\n",
    "import os\n",
    "import jieba # pip install jieba\n",
    "\n",
    "# input files\n",
    "train_file = '../../text_classification_data/cnews.train.txt'\n",
    "val_file = '../../text_classification_data/cnews.val.txt'\n",
    "test_file = '../../text_classification_data/cnews.test.txt'\n",
    "\n",
    "# output files\n",
    "seg_train_file = '../../text_classification_data/cnews.train.seg.txt'\n",
    "seg_val_file = '../../text_classification_data/cnews.val.seg.txt'\n",
    "seg_test_file = '../../text_classification_data/cnews.test.seg.txt'\n",
    "\n",
    "vocab_file = '../../text_classification_data/cnews.vocab.txt'\n",
    "category_file = '../../text_classification_data/cnews.category.txt'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(val_file, 'r') as f:\n",
    "    lines = f.readlines()\n",
    "\n",
    "label, content = lines[0].decode('utf-8').strip('\\r\\n').split('\\t')\n",
    "word_iter = jieba.cut(content)\n",
    "\n",
    "print content\n",
    "print '/ '.join(word_iter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_seg_file(input_file, output_seg_file):\n",
    "    \"\"\"Segment the sentences in each line in input_file\"\"\"\n",
    "    with open(input_file, 'r') as f:\n",
    "        lines = f.readlines()\n",
    "    with open(output_seg_file, 'w') as f:\n",
    "        for line in lines:\n",
    "            label, content = line.decode('utf-8').strip('\\r\\n').split('\\t')\n",
    "            word_iter = jieba.cut(content)\n",
    "            word_content = ''\n",
    "            for word in word_iter:\n",
    "                word = word.strip(' ')\n",
    "                if word != '':\n",
    "                    word_content += word + ' '\n",
    "            out_line = '%s\\t%s\\n' % (label, word_content.strip(' '))\n",
    "            f.write(out_line.encode('utf-8'))\n",
    "\n",
    "generate_seg_file(train_file, seg_train_file)\n",
    "generate_seg_file(val_file, seg_val_file)\n",
    "generate_seg_file(test_file, seg_test_file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_vocab_file(input_seg_file, output_vocab_file):\n",
    "    with open(input_seg_file, 'r') as f:\n",
    "        lines = f.readlines()\n",
    "    word_dict = {}\n",
    "    for line in lines:\n",
    "        label, content = line.strip('\\r\\n').decode('utf-8').split('\\t')\n",
    "        for word in content.split():\n",
    "            word_dict.setdefault(word, 0)\n",
    "            word_dict[word] += 1\n",
    "    # [(word, frequency), ..., ()]\n",
    "    sorted_word_dict = sorted(\n",
    "        word_dict.items(), key = lambda d:d[1], reverse=True)\n",
    "    with open(output_vocab_file, 'w') as f:\n",
    "        f.write('<UNK>\\t10000000\\n')\n",
    "        for item in sorted_word_dict:\n",
    "            f.write('%s\\t%d\\n' % (item[0].encode('utf-8'), item[1]))\n",
    "\n",
    "generate_vocab_file(seg_train_file, vocab_file)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_category_dict(input_file, category_file):\n",
    "    with open(input_file, 'r') as f:\n",
    "        lines = f.readlines()\n",
    "    category_dict = {}\n",
    "    for line in lines:\n",
    "        label, content = line.strip('\\r\\n').decode('utf-8').split('\\t')\n",
    "        category_dict.setdefault(label, 0)\n",
    "        category_dict[label] += 1\n",
    "    category_number = len(category_dict)\n",
    "    with open(category_file, 'w') as f:\n",
    "        for category in category_dict:\n",
    "            line = '%s\\n' % category.encode('utf-8')\n",
    "            print '%s\\t%d' % (\n",
    "                category.encode('utf-8'), category_dict[category])\n",
    "            f.write(line)\n",
    "            \n",
    "generate_category_dict(train_file, category_file)\n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
